﻿using System;
using System.Linq;
using System.Web.Mvc;
using MM.Data.Entity;
using MM.Services.decl;
using MM.Web.Models;

namespace MM.Web.Controllers
{
    /// <summary>
    /// Default controller
    /// </summary>
    public class MyMoneyController : Controller
    {
        private readonly IJournalPayment _journalPaymentService;
        private readonly IBalanceInterface _balanceService;
        private readonly ICurrencyService _currencyService;
        private readonly IOperationTypeInterface _operationTypeService;
        private readonly IPrioritetInterface _prioritetService;

        /// <summary>
        /// Initializes a new instance of the <see cref="MyMoneyController"/> class.
        /// </summary>
        public MyMoneyController()
        {
            
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MyMoneyController"/> class.
        /// </summary>
        /// <param name="journalPaymentService">The journal payment service.</param>
        /// <param name="balanceService">The balance service.</param>
        /// <param name="currencyService">The currency service.</param>
        /// <param name="operationTypeService">The operation type service.</param>
        /// <param name="prioritetService">The prioritet service.</param>
        public MyMoneyController(IJournalPayment journalPaymentService, IBalanceInterface balanceService,
            ICurrencyService currencyService, IOperationTypeInterface operationTypeService,
            IPrioritetInterface prioritetService)
        {
            _journalPaymentService = journalPaymentService;
            _balanceService = balanceService;
            _currencyService = currencyService;
            _operationTypeService = operationTypeService;
            _prioritetService = prioritetService;
        }

        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
                var model = new PaymentModel
                                {
                                    JournalPayments =
                                        _journalPaymentService.GetAllJournalPaymentsByDate(DateTime.Now).ToList(),
                                };

                return View(model);
            
        }

        /// <summary>
        /// Adds the new payment. (GET)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult CreateNewPayment()
        {
                return
                    Json(
                        new
                            {
                                OperationTypes = _operationTypeService.GetAllOperationTypesHierarchical().ToList(),
                                Priorities = _prioritetService.GetAllPriorities().ToList(),
                                Currency = _currencyService.GetAllCurrincyList().ToList()
                            }, JsonRequestBehavior.AllowGet);
            
        }

        /// <summary>
        /// Adds the new payment. (POST)
        /// </summary>
        /// <param name="journalPayment">The journal payment.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddNewPayment(JournalPayment journalPayment)
        {
                _journalPaymentService.AddJournalPayment(journalPayment);

                return
                    Json(
                        new
                            {
                                journalPayment
                            });
            
        }

        /// <summary>
        /// Adds the new operation type. (GET)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult CreateNewOperationType()
        {
                return
                    Json(
                        new
                            {
                                ParentOperationTypes =
                            _operationTypeService.GetAllParentOperationList().ToList()
                            }, JsonRequestBehavior.AllowGet);
            
        }

        /// <summary>
        /// Anns the new type of the operation.
        /// </summary>
        /// <param name="operationType">Type of the operation.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddNewOperationType(OperationType operationType)
        {
                operationType.Parent = _operationTypeService.GetParentOperation(operationType);

                if (operationType.Parent != null)
                    operationType.LevelDepth = operationType.Parent.LevelDepth + 1;

                _operationTypeService.AddOperationType(operationType);

                return
                    Json(
                        new
                        {
                            operationType.OperationTypeId,
                            operationType.NameLong,
                            isZeroLevelO = operationType.LevelDepth == 0
                        });
            }
        
    }
}